package com.meshnetics.gb.util.checkers;

/**
 * Base class for unbroken (continuous) range checkers.
 *
 * @author Yury Aserchev (yaserchev@meshnetics.com)
 *         Date: 23.06.2008 16:27:47
 */
public abstract class AbstractRangeChecker implements RangeCheck {

    /**
     * Checks if value is in expected countinuous (unbroken) range from
     * allowed Min value to allowed Max value.
     *
     * @param value checking value.
     * @throws IllegalArgumentException if the give value is not withing the expected range
     */
    public void checkRange(final long value) {
        final long min = getMin();
        final long max = getMax();

        if (value < min) {
            throw new IllegalArgumentException("Passed value " + String.valueOf(value) +
                    " is less than minimal " + String.valueOf(min));
        } else if (value > max) {
            throw new IllegalArgumentException("Passed value " + String.valueOf(value) +
                    " is greater than maximal " + String.valueOf(max));
        }
    }

    /**
     * Return allowed minimal value of range.
     * @return min value.
     */
    protected abstract long getMin();

    /**
     * Return allowed maximal value of range.
     * @return max value.
     */
    protected abstract long getMax();
}
